#include "stdafx.h"
#include "buildmenu.h"


buildmenu::buildmenuResult buildmenu::Show(sf::RenderWindow& window)
{

	//Load menu image from file
	//sf::Image image;
	//image.LoadFromFile("images/menuutama.jpg");
	//sf::Sprite sprite(image);

	//Setup clickable regions

	//Play menu item coordinates
	buildmenuItem startButton;
	startButton.rect.Top= 28;
	startButton.rect.Bottom = 64;
	startButton.rect.Left = 518;
	startButton.rect.Right = 630;
	startButton.action = Start;

	//Exit menu item coordinates
	buildmenuItem exitButton;
	exitButton.rect.Left = 518;
	exitButton.rect.Right = 630;
	exitButton.rect.Top = 80;
	exitButton.rect.Bottom = 120;
	exitButton.action = Exit;

	buildmenuItem T1Button;
	T1Button.rect.Left = 565;
	T1Button.rect.Right = 585;
	T1Button.rect.Top = 135;
	T1Button.rect.Bottom = 170;
	T1Button.action = tower1;

	buildmenuItem T2Button;
	T2Button.rect.Left = 565;
	T2Button.rect.Right = 585;
	T2Button.rect.Top = 185;
	T2Button.rect.Bottom = 215;
	T2Button.action = tower2;

	buildmenuItem T3Button;
	T3Button.rect.Left = 565;
	T3Button.rect.Right = 585;
	T3Button.rect.Top = 230;
	T3Button.rect.Bottom = 255;
	T3Button.action = tower3;

	_buildmenuItem.push_back(startButton);
	_buildmenuItem.push_back(exitButton);
	_buildmenuItem.push_back(T1Button);
	_buildmenuItem.push_back(T2Button);
	_buildmenuItem.push_back(T3Button);

	//window.Draw(sprite);
	//window.Display();

	return GetMenuResponse(window);
}

buildmenu::buildmenuResult buildmenu::HandleClick(int x, int y)
{
	std::list<buildmenuItem>::iterator it;

	for ( it = _buildmenuItem.begin(); it != _buildmenuItem.end(); it++)
	{
		sf::Rect<int> menuItemRect = (*it).rect;
		if( menuItemRect.Bottom > y 
			&& menuItemRect.Top < y 
			&& menuItemRect.Left < x 
			&& menuItemRect.Right > x)
			{
				return (*it).action;
			}
	}

	return Nothing;
}

buildmenu::buildmenuResult  buildmenu::GetMenuResponse(sf::RenderWindow& window)
{
	sf::Event menuEvent;

	while(42 != 43)
	{

		while(window.GetEvent(menuEvent))
		{
			if(menuEvent.Type == sf::Event::MouseButtonPressed)
			{
				return HandleClick(menuEvent.MouseButton.X,menuEvent.MouseButton.Y);
			}
			if(menuEvent.Type == sf::Event::Closed)
			{
				return Exit;
			}
		}
	}
}